package com.taiji.dianthus.api.sys;

import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sys.DSysMenu;
import com.taiji.dianthus.service.sys.MenuService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_DEL;

/**
 * @ClassName SysMenuController
 * @Description 系统菜单权限api
 * @Author H.M
 * @Date 2020/1/4
 */
@Api(value = "系统菜单api")
@RestController
@RequestMapping(value = "/api/menu")
public class SysMenuController extends BasicController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private SysLogService sysLogService;


    @RequestMapping(value = "/save")
    @ResponseBody
    @ApiOperation(value = "保存/修改菜单", notes = "菜单保存/修改接口")
    @ApiImplicitParam(name = "form", value = "保存/修改菜单", required = true, dataType = "DSysMenu")
    public BusinessMessage saveMenu(@Valid  @RequestBody DSysMenu menu) {
        return menuService.saveMenu(menu);
    }


    @RequestMapping(value = "/delete")
    @ResponseBody
    @ApiOperation(value = "删除菜单", notes = "删除菜单接口")
    @ApiImplicitParam(name="id",value="主键",dataType="string", paramType = "query")
    public BusinessMessage deleteMenu(HttpServletRequest request,String id) {
        DSysMenu menu = menuService.getByMenuId(id);
        BusinessMessage message = menuService.deleteMenu(id);
        sysLogService.save(currentUser(), LOG_OPERATION_DEL, IPUtil.getIpAddr(request),
                "删除菜单[" + menu.getMenuName() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        return message;
    }
}
